﻿Public Class ResourceForm
    Private parentForm As SearchResourcesForm = New SearchResourcesForm

    Enum state
        insert
        edit
    End Enum

    Dim action As state = state.insert

    Public Sub ResourceForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        GenericAdministrator.loadComboBox(Me.cmbDocumentType, GenericAdministrator.searchDataByFilter(New DocumentType), "id", "name")
        If parentForm.idSelected <> Nothing Then
            loadFormToEdit(parentForm.idSelected)
            action = state.edit
            Me.txtDocument.Enabled = False
            Me.cmbDocumentType.Enabled = False
        End If
    End Sub

    Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
        Dim resource As Resource = Nothing
        Dim hourCost = Util.convertStringToDouble(Me.txtHourCost.Text)

        If Me.validateRequiredFields() Then
            If Me.action = state.insert Then
                If Me.validateDuplicatedResource = True Then
                    resource = New Resource(Nothing, Me.txtFirstname.Text, Me.txtSurname.Text, Me.cmbDocumentType.SelectedValue, CType(Me.txtDocument.Text, Integer), Me.txtEmail.Text, Nothing, Nothing, hourCost)
                    ResourceAdministrator.insert(resource)
                    Me.Close()
                    Dim searchResourcesForm As New SearchResourcesForm()
                    searchResourcesForm.Show()
                End If
            Else
                resource = New Resource(parentForm.idSelected, Me.txtFirstname.Text, Me.txtSurname.Text, Nothing, Nothing, Me.txtEmail.Text, Nothing, Nothing, hourCost)
                ResourceAdministrator.update(resource)
                Me.Close()
                Dim searchResourcesForm As New SearchResourcesForm()
                searchResourcesForm.Show()
            End If

        End If
    End Sub

    Public Sub New()
        InitializeComponent()
    End Sub

    Public Sub New(ByRef parentFormParam As SearchResourcesForm)
        InitializeComponent()
        parentForm = parentFormParam
    End Sub

    Private Function validateRequiredFields()
        If Me.txtSurname.Text = "" Then
            MsgBox("Apellido es Requerido", vbCritical, "Importante")
            Me.txtSurname.Focus()
            Return False
        End If

        If Me.txtFirstname.Text = "" Then
            MsgBox("Nombres es Requerido", vbCritical, "Importante")
            Me.txtFirstname.Focus()
            Return False
        End If

        If Me.cmbDocumentType.SelectedIndex = -1 Then
            MsgBox("Tipo de Documento es Requerido", vbCritical, "Importante")
            Me.cmbDocumentType.Focus()
            Return False
        End If

        If Me.txtDocument.Text = "" Then
            MsgBox("Documento es Requerido", vbCritical, "Importante")
            Me.txtDocument.Focus()
            Return False
        End If

        Return True
    End Function

    Private Sub txtDocument_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDocument.KeyPress
        ViewValidators.isInteger(e)
    End Sub

    Private Sub txtHourCost_KeyPress(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtHourCost.KeyPress
        ViewValidators.isFloat(e)
    End Sub

    Private Sub loadFormToEdit(ByVal id As Integer)
        Dim table As Data.DataTable = ResourceAdministrator.searchDataById("*", id)
        Me.txtFirstname.Text = table.Rows(0)("firstname")
        Me.txtSurname.Text = table.Rows(0)("surname")
        Me.cmbDocumentType.SelectedValue = table.Rows(0)("document_type")
        Me.txtDocument.Text = table.Rows(0)("document_number")
        Me.txtHourCost.Text = table.Rows(0)("hour_cost")
        Me.txtEmail.Text = table.Rows(0)("email")
    End Sub

    Private Function validateDuplicatedResource()
        Dim resource As New Resource(Nothing, Nothing, Nothing, Me.cmbDocumentType.SelectedValue, CType(Me.txtDocument.Text, Integer), Nothing, Nothing, Nothing, Nothing)
        Dim table As Data.DataTable = ResourceAdministrator.searchDataByFilter(resource)
        If table.Rows.Count > 0 Then
            MsgBox("El recurso ya existe en la base de datos.", vbCritical, "Importante")
            Return False
        End If
        Return True
    End Function

    Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancel.Click
        Me.Close()
    End Sub
End Class
